package GUI.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.tree.DefaultMutableTreeNode;

import GUI.models.TreeNodeImplProperties;

import client.domainModels.Node.Node;
import client.domainModels.NodeImpl.AbstractNode;
import client.domainModels.NodeImpl.Case;
import client.domainModels.NodeImpl.Event;
import client.domainModels.NodeImpl.Task;

/**
*
* @author Wei WANG
*/

public class XMLFileTreePanel extends JPanel 
                             implements ActionListener {
    private static String ADD_COMMAND = "add";
    private static String REMOVE_COMMAND = "remove";
    private static String MODIFY_COMMAND = "Modify";
    private static String CLEAR_COMMAND = "clear";
    
    private JPanel panelConf;
    
    private XMLFileTree treePanel;
    private JPanel tablesPanel;

    public XMLFileTreePanel(JPanel tablesPanel) {
        super(new BorderLayout());
        this.tablesPanel = tablesPanel;
        //Create the components.
        treePanel = new XMLFileTree();
       // populateTree(treePanel);
        
        JButton addButton = new JButton("Add");
        addButton.setActionCommand(ADD_COMMAND);
        addButton.addActionListener(this);
        
        JButton removeButton = new JButton("Remove");
        removeButton.setActionCommand(REMOVE_COMMAND);
        removeButton.addActionListener(this);
        
        JButton modifyButton = new JButton("Modify");
        modifyButton.setActionCommand(MODIFY_COMMAND);
        modifyButton.addActionListener(this);
        
        JButton clearButton = new JButton("Clear");
        clearButton.setActionCommand(CLEAR_COMMAND);
        clearButton.addActionListener(this);
        
        

        //Lay everything out.
        treePanel.setPreferredSize(new Dimension(300, 150));
        add(treePanel, BorderLayout.WEST);

        JPanel panelEast = new JPanel(new BorderLayout());
        panelEast.setPreferredSize(new Dimension(300, 300));
        
        JPanel panel = new JPanel(new GridLayout(0,4));
        panel.add(addButton);
        panel.add(removeButton); 
        panel.add(modifyButton);
        panel.add(clearButton);
        panelEast.add(panel, BorderLayout.NORTH);
        
        panelConf = new JPanel();

        panelEast.add(panelConf, BorderLayout.CENTER);
        JScrollPane scrollPanel = new JScrollPane(panelEast);
        
	
        add(scrollPanel, BorderLayout.CENTER);
    }

    public XMLFileTree getXMLFileTree(){
    	return treePanel;
    }
    
    public void populateTree(XMLFileTree treePanel) {
    	
    	
    	AbstractNode case1 = new Case();
    	case1.setID("case1");
        
    	AbstractNode case2 = new Case();
        case2.setID("case2");
      
        
        TreeNodeImplProperties p1, p2;

        p1 = treePanel.addObject(null, case1);
        p2 = treePanel.addObject(null, case2);

     /*   treePanel.addObject(p1, event1);
        treePanel.addObject(p1, event2);*/

    }
    
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        
        if (ADD_COMMAND.equals(command)) {
        	JPanel panel = new TreeConfAddPanelFormule(treePanel,tablesPanel);
        	panelConf.removeAll();
        	panelConf.add(panel,BorderLayout.CENTER);      	
        	this.updateUI();
           
        } else if (REMOVE_COMMAND.equals(command)) {
            //Remove button clicked
        	String nodeType=treePanel.getLastNode().getPropretiesObj().getClass().getSimpleName();
        	if(nodeType.equals("Event")||nodeType.equals("Case")){
        		treePanel.removeCurrentNode();
                JScrollPane scrollPanel = new JScrollPane(MultiLineTableConstructor.createLogTable(treePanel.getRootNode()));
    			tablesPanel.removeAll();
    			tablesPanel.add(scrollPanel,BorderLayout.CENTER);
    			tablesPanel.updateUI();
        	} else {
    			Object[] options = { "OK", "Cancel" };
    			int n = JOptionPane.showOptionDialog(this,
    					"You can only delete a case or a event!", "Attetion",
    					JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
    					null, // do not use a custom Icon
    					options, // the titles of buttons
    					options[0]); // default button title
    		}
        } else if (MODIFY_COMMAND.equals(command)) {
            //Modify button clicked.
        	JPanel panel = new TreeConfModifyPanelFormule(treePanel,tablesPanel);
        	panelConf.removeAll();
        	panelConf.add(panel,BorderLayout.CENTER);      	
        	this.updateUI();
        }else if (CLEAR_COMMAND.equals(command)) {
            //Clear button clicked.
            treePanel.clear();
            JScrollPane scrollPanel = new JScrollPane(MultiLineTableConstructor.createLogTable(treePanel.getRootNode()));
			tablesPanel.removeAll();
			tablesPanel.add(scrollPanel,BorderLayout.CENTER);
			tablesPanel.updateUI();
        }
    }

}
